package com.k.kpp;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.Properties;

/**
 * @author lang
 */
@Configuration
public class ValidatorConfig {
    @Resource
    private MessageSource messageSource;
    @Bean
    public Validator validator(){
        // JKD way
        /*ValidatorFactory validatorFactory = Validation.byProvider( org.hibernate.validator.HibernateValidator.class )
                .configure()
                .addProperty( "hibernate.validator.fail_fast", "false" )
                .buildValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        return validator;*/

        // Spring way 注入messageSource I18N
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setProviderClass(org.hibernate.validator.HibernateValidator.class);
        bean.setValidationMessageSource(messageSource);
        Properties properties = new Properties();
        properties.setProperty("hibernate.validator.fail_fast", "false");
        bean.setValidationProperties(properties);
        return bean;
    }
}